﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace produccion.clases
{
    class CLSFAVUSUP
    {
    
//Maestro menu favoritos
#region Metodos basicos tabla

        MDOgeneral mod = new MDOgeneral();

        public DataTable consultar()
        {   
            try{
            string query = "select c.CONSEC_FUS,c.CODHIJO_FUS,c.NOMBRE_OPC, c.FORMUL_OPC,d.CODUSU_OPU from (select a.CONSEC_FUS,a.CODUSU_FUS,a.CODHIJO_FUS,b.NOMBRE_OPC,b.FORMUL_OPC from FAVUSUP a inner join MAEOPCP b on (a.CODHIJO_FUS=b.CODHIJO_OPC) where (a.CODUSU_FUS='" + MDOgeneral.usuario + "')) c left outer join OPCUSUP d on (c.CODHIJO_FUS=d.CODHIJO_OPU and c.CODUSU_FUS=d.CODUSU_OPU) order by c.CONSEC_FUS";
            return mod.consulta(query);
            }
            catch (Exception e) { throw e; }
        }
        public int eliminar()
        {
            try{
            int f=0;
            string query = "delete from FAVUSUP where CODUSU_FUS='" + MDOgeneral.usuario + "' and CODHIJO_FUS=" + CODHIJO_FUS;
            if(mod.comando(query)>0){
            query="update FAVUSUP set CONSEC_FUS=(CONSEC_FUS-1) where ((CODUSU_FUS='"+MDOgeneral.usuario+"') and (CONSEC_FUS>="+CONSEC_FUS+"))";
            mod.comando(query);
            f = 1;
            }
            return f;
            }catch (Exception e) { throw e; }
        }
   
        public int insertar()
        {
            try{
            if (!existe())
            {
                string query = "insert into FAVUSUP (CODUSU_FUS,CONSEC_FUS,CODHIJO_FUS) VALUES ('" + MDOgeneral.usuario + "',(select coalesce(max(consec_fus)+1,1) from FAVUSUP where CODUSU_FUS='" + MDOgeneral.usuario + "')," + CODHIJO_FUS + ")";
                return mod.comando(query);
            }

            return 0;
            }catch (Exception e) { throw e; }
        }

        public int ordenar(int conse_dest)
        {
            try{
            string query = "update FAVUSUP set CONSEC_FUS=0 where ((CODUSU_FUS='" + MDOgeneral.usuario + "') and (CODHIJO_FUS=" + CODHIJO_FUS + "))";
            mod.comando(query);

            if (CONSEC_FUS > conse_dest)
            {
                query = "update FAVUSUP set CONSEC_FUS=(CONSEC_FUS+1) where ((CODUSU_FUS='" + MDOgeneral.usuario + "') and (CODHIJO_FUS!=0) and (CONSEC_FUS>=" + conse_dest + ") and (CONSEC_FUS<=" + CONSEC_FUS + "))";
            }
            else
            {
                query = "update FAVUSUP set CONSEC_FUS=(CONSEC_FUS-1) where ((CODUSU_FUS='" + MDOgeneral.usuario + "') and (CODHIJO_FUS!=0) and (CONSEC_FUS>=" + CONSEC_FUS + ") and (CONSEC_FUS<=" + conse_dest + "))";
            }
            mod.comando(query);
            query = "update FAVUSUP set CONSEC_FUS=" + conse_dest + " where ((CODUSU_FUS='" + MDOgeneral.usuario + "') and (CODHIJO_FUS=" + CODHIJO_FUS + "))";
            
            return mod.comando(query);
            }catch (Exception e) { throw e; }
        }

        public Boolean existe() {
           try{
           string query = "select coalesce (count(codhijo_fus),0) from FAVUSUP where  CODUSU_FUS='" + MDOgeneral.usuario + "' and CODHIJO_FUS=" + CODHIJO_FUS;
           DataTable val= mod.consulta(query);
           if (val.Rows[0][0].ToString().Equals("0"))
           {
               return false;    
           }
           return true;
           }catch (Exception e) { throw e; }
        
        }
        #endregion

#region  Propiedades tabla

        private string _CODUSU_FUS;
        public string CODUSU_FUS
        {
            get { return _CODUSU_FUS; }
            set { _CODUSU_FUS = value; }
        }
        private int _CONSEC_FUS;
        public int CONSEC_FUS
        {
            get { return _CONSEC_FUS; }
            set { _CONSEC_FUS = value; }
        }
        private int _CODHIJO_FUS;
        public int CODHIJO_FUS
        {
            get { return _CODHIJO_FUS; }
            set { _CODHIJO_FUS = value; }
        }

#endregion

       




    }
}
